/*
 * Created on 2005-11-14
 *
 */
package com.yl.cap.analyst.report.chart.maker;

import java.awt.Color;
import java.io.FileOutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis3D;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.yl.cap.analyst.report.chart.data.ChartData;
import com.yl.cap.analyst.report.chart.data.DualAxisChartData;

public class DualAxis3DMaker extends ChartMaker
{

    public void makeChart(String fileName, ChartData data) throws Exception
    {
        DualAxisChartData chartData = (DualAxisChartData)data;
        //create dataset
        CategoryDataset barDataset = DatasetUtilities.createCategoryDataset(
                chartData.getBarData().getRowKeys(), chartData.getBarData().getColumnKeys(), chartData.getBarData().getData());
        CategoryDataset lineDataSet = DatasetUtilities.createCategoryDataset(
                chartData.getLineData().getRowKeys(), chartData.getLineData().getColumnKeys(), chartData.getLineData().getData());
        //=========================================
        JFreeChart jfreechart = ChartFactory.createBarChart3D("Dual Axis Chart", "Category", "Value", barDataset, PlotOrientation.VERTICAL, true, true, false);
        jfreechart.setBackgroundPaint(new Color(204, 255, 204));
        CategoryPlot categoryplot = jfreechart.getCategoryPlot();
        categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        categoryplot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);
        CategoryItemRenderer categoryitemrenderer = categoryplot.getRenderer();
        categoryitemrenderer.setSeriesPaint(0, Color.red);
        categoryitemrenderer.setSeriesPaint(1, Color.yellow);
        categoryitemrenderer.setSeriesPaint(2, Color.green);
        NumberAxis3D numberaxis3d = new NumberAxis3D(chartData.getZCaption());
        categoryplot.setRangeAxis(1, numberaxis3d);
        categoryplot.setDataset(1, lineDataSet);
        categoryplot.mapDatasetToRangeAxis(1, 1);
        LineRenderer3D linerenderer3d = new LineRenderer3D();
        linerenderer3d.setSeriesPaint(0, Color.blue);
        categoryplot.setRenderer(1, linerenderer3d);
        categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        //===========================
        FileOutputStream fos_jpg = null;
        try
        {
            fos_jpg = new FileOutputStream(fileName);
            ChartUtilities.writeChartAsJPEG(fos_jpg, 100, jfreechart, data
                    .getWidth(), data.getHeight(), null);
        }
        catch (Exception e)
        {
            fos_jpg.close();
            throw e;
        }
        fos_jpg.close();
    }

}
